package com.services;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.conn.DbCES;

public class CESDate {
	
	public static String swithDate(String date, String dbname) {
		return swithDate(date, DbCES.getConnection(dbname));
	}

	public static String swithDate(String date, Connection conn) {
		if (date == null || "".equals(date)) {
			return "请输入日期";
		}
		if (!date.matches("\\d{4}-\\d{2}-\\d{2}")) {
			return "日期格式为YYYY-MM-DD";
		}
		changeDate(date, conn);
		return "";
	}
	
	public static String getSysDate(String dbname) {
		return getSysDate(DbCES.getConnection(dbname));
	}
	
	public static String getSysDate(Connection conn) {
		String sql = "select paramvalue from sys_param where paramid='0001'";
		Statement stmt;
		ResultSet rs;
		String result = "";
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			if (rs.next()) {
				result = rs.getString("paramvalue");
			}
			rs.close();
			stmt.close();
		} catch (Exception e) {
			rs = null;
			stmt = null;
		}
		return result;
	}

	private static void changeDate(String date, Connection conn) {
		String sql = "update sys_param set paramvalue='" + date + "' where paramid='0001'";
		Statement stmt;
		try {
			stmt = conn.createStatement();
			stmt.executeUpdate(sql);
			stmt.close();
		} catch (Exception e) {
			stmt = null;
		}
	}
}
